//
// Created by luolijun on 2021/11/1.
//

#include "FacadeRepositoryWidget.h"

#include "MainWindow.h"

#include <QCursor>
#include <QTreeWidgetItem>
#include <QVariant>

namespace falcon {

FacadeRepositoryWidget::FacadeRepositoryWidget(QWidget* parent)
    : QTreeWidget(parent) {

    connect(this, &FacadeRepositoryWidget::itemDoubleClicked, this, &FacadeRepositoryWidget::createPaddingItem);
}

void FacadeRepositoryWidget::attach(FalconDocument* doc) {
    //  清除所有当前的数据
    this->clear();

    this->falconDocument = doc;

    doc->facadeRepo->enumPrototypeGroup([this, doc](std::string groupName) -> int {
        QTreeWidgetItem* groupItem = new QTreeWidgetItem(QStringList() << groupName.c_str());
        this->addTopLevelItem(groupItem);
        doc->facadeRepo->enumPrototype(groupName, [groupItem](falcon::Facade* fc) -> int {
            QTreeWidgetItem* childItem = new QTreeWidgetItem(QStringList() << fc->object()->type.c_str());
            groupItem->addChild(childItem);
            childItem->setData(0, Qt::UserRole, QVariant::fromValue((void*)fc));
            return 0;
        });
        return 0;
    });
}

void FacadeRepositoryWidget::createPaddingItem(QTreeWidgetItem* item, int column) {
    falcon::Facade* fc = (falcon::Facade*)(item->data(0, Qt::UserRole).value<void*>());
    falconDocument->pendingFacade = fc;
}

} // namespace falcon